%% Graph: Share Matrices

    % Share matrices (data)
    graphstr = '../../output/figures/Figure4/matrix_data_';
    myplot_matrix(data.share_outdegree_d,'customer',0.95,graphstr)
    myplot_matrix(data.share_sales_d,'sales',0.95,graphstr)
    myplot_matrix2(data.share_indegree_d,'supplier',0.95,graphstr)
    myplot_matrix2(data.share_spend_d,'spending',0.95,graphstr)

    % Share matrics (model)
    graphstr = '../../output/figures/Figure4/matrix_model_';
    myplot_matrix(model.share_outdegree_d,'customer',0.95,graphstr)
    myplot_matrix(model.share_sales_d,'sales',0.95,graphstr)
    myplot_matrix2(model.share_indegree_d,'supplier',0.95,graphstr)
    myplot_matrix2(model.share_spend_d,'spending',0.95,graphstr)   
    
    
   
%% Function: 3D Bar plot
function myplot_matrix(matrix_data,name,zmax,commonstr)

    % Plot
    fig = figure('visible','off','DefaultAxesFontSize',14);  
    h = bar3(matrix_data,0.5);
    zlim([0,zmax])
    xlabel('Supplier')
    ylabel('Buyer')
    zticks([0 0.2 0.4 0.6 0.8])
    view(-145,20)
    
    % adjust color
%     mycolor = [ 0, 0.4470, 0.7410;
%                 0.8500, 0.3250, 0.0980;
%                 0.9290, 0.6940, 0.1250;
%                 0.4940, 0.1840, 0.5560;
%                 0.4660, 0.6740, 0.1880; ];
    mycolor = [ 0.3, 0.3, 0.3;
                0.7, 0.7, 0.7;
                0.45, 0.45, 0.45;
                0.9, 0.9, 0.9;
                0.6, 0.6, 0.6; ];
    for i = 1:5
        set(h(i), 'facecolor', mycolor(i,:))
    end
         
    % Save
    %filename = strcat(commonstr,name,'_share.png');
    %exportgraphics(fig,filename)    
    set(gcf,'renderer','Painters')
    filename = strcat(commonstr,name,'_share.pdf');
    exportgraphics(fig,filename)  
    
end
function myplot_matrix2(matrix_data,name,zmax,commonstr)

    % Plot
    fig = figure('visible','off','DefaultAxesFontSize',14);  
    h = bar3(matrix_data,0.5);   
    zlim([0,zmax])
    xlabel('Supplier')
    ylabel('Buyer')
    zticks([0 0.2 0.4 0.6 0.8])
    view(-145,20)
    
    % adjust color
%     mycolor = [ 0, 0.4470, 0.7410;
%                 0.8500, 0.3250, 0.0980;
%                 0.9290, 0.6940, 0.1250;
%                 0.4940, 0.1840, 0.5560;
%                 0.4660, 0.6740, 0.1880; ];
    mycolor = [ 0.3, 0.3, 0.3;
                0.7, 0.7, 0.7;
                0.45, 0.45, 0.45;
                0.9, 0.9, 0.9;
                0.6, 0.6, 0.6; ];    
    cnt = 0;
    for jj = 1:length(h)
        xd = get(h(jj),'xdata');
        yd = get(h(jj),'ydata');
        zd = get(h(jj),'zdata');
        delete(h(jj))    
        idx = [0;find(all(isnan(xd),2))];
        if jj == 1
            S = zeros(length(h)*(length(idx)-1),1);
        end
        for ii = 1:length(idx)-1
            cnt = cnt + 1;
            S(cnt) = surface(xd(idx(ii)+1:idx(ii+1)-1,:),...
                             yd(idx(ii)+1:idx(ii+1)-1,:),...
                             zd(idx(ii)+1:idx(ii+1)-1,:),...
                             'facecolor',mycolor(ii,:));
        end
    end    
  
    % Save
    %filename = strcat(commonstr,name,'_share.png');
    %exportgraphics(fig,filename)    
    set(gcf,'renderer','Painters')
    filename = strcat(commonstr,name,'_share.pdf');
    exportgraphics(fig,filename) 
    
end    